#ifndef UI_SESSION_H
#define UI_SESSION_H

#include <atomic>
#include <memory>
#include <mutex>
#include <thread>

#include <crow/json.h>

#include "common/elements/user.h"
#include "common/more/uuid.h"

#include "cash_manager.hpp"
#include "ui_session_message.h"

namespace Api
{
class Client;
} // namespace Api

class UiSession final
{
public:
    UiSession(
        const Uuids::Uuid& sessionUuid,
        unsigned userId,
        std::shared_ptr<Api::Client> datenaroGate
    );
    ~UiSession();

    CashManager& cashManager();

    Elements::UserPtr user() const;
    crow::json::wvalue translation() const;

    void forceUpdate();

    UiSessionMessageList& sessionMessages();

private:
    void loadPermanentElements();
    void updateLoop();
    void update();

private:
    const Uuids::Uuid m_sessionUuid;
    const unsigned m_userId;

private:
    CashManager m_cashManager;
    std::shared_ptr<Api::Client> m_datenaroGate;

    std::unique_ptr<std::thread> m_permanentElementsUpdateThread;
    std::atomic_bool m_isRunning { true };
    std::mutex m_updateLock;

    UiSessionMessageList m_sessionMessages;
};

typedef std::shared_ptr<UiSession> UiSessionPtr;

#endif // UI_SESSION_H
